﻿using MediatR;
using T.Contract.API.Application.Command.ContractCommand;
using T.Contract.Repository.Interface;

namespace T.Contract.API.Application.CommandHandlers.ContractHandler
{
    /// <summary>
    /// 审批货主合同
    /// </summary>
    public class ApprovalRecordCreateCommandHanders : IRequestHandler<ApprovalRecorddCreateCommand, int>
    {
        private readonly IHApproval _hApproval;
        private readonly IContractRepository _contractRepository;
        public ApprovalRecordCreateCommandHanders(IHApproval hApproval, IContractRepository contractRepository)
        {
            _hApproval = hApproval;
            _contractRepository = contractRepository;
        }

        public async Task<int> Handle(ApprovalRecorddCreateCommand request, CancellationToken cancellationToken)
        {
         
            var sss = await _contractRepository.GetModelAsync((int)request.ContractId);
            if (request.ApprovalResult == "同意")
            {
                await _hApproval.AddAsync(request);
                sss.CcontractStatus = 3;
                sss.Approver = request.Approver;
               await _contractRepository.UpdateAsync(sss);


            }
            if (request.ApprovalResult == "同意并转批")
            {
               await _hApproval.AddAsync(request);
            }

            if (request.ApprovalResult == "拒绝")
            {
               await _hApproval.AddAsync(request);
                sss.CcontractStatus = 4;
                sss.Approver = request.Approver;
               await _contractRepository.UpdateAsync(sss);
            }
            return 1;
        }
    }
}
